package com.neuedu.his.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.his.po.Department;
import com.neuedu.his.service.DepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/departments")
public class DepartmentController {

    private static final Logger log = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private DepartmentService departmentService;

    /**
     * 获取所有科室列表（供前端选择检验科室）
     */
    @GetMapping
    public ResponseEntity<List<Department>> getAllDepartments() {
        try {
            log.info("开始查询所有科室");

            // 查询所有可用科室（假设DelMark=0表示可用）
            QueryWrapper<Department> wrapper = new QueryWrapper<>();
            wrapper.eq("DelMark", 0);

            List<Department> departments = departmentService.list(wrapper);
            log.info("获取科室列表成功，数量={}", departments.size());

            return ResponseEntity.ok(departments);
        } catch (Exception e) {
            log.error("获取科室列表异常", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}